package com.escom.activity;

import android.app.ActionBar;
import android.app.FragmentTransaction;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;
import android.view.Menu;
import android.view.MenuItem;

import com.escom.R;
import com.escom.utils.Constantes;
import com.escom.utils.TabsAdapterEstudio;

/**
 * Created by Carlos on 19/04/2014.
 */
public class EstudioActivity extends FragmentActivity implements ActionBar.TabListener {

    private ViewPager viewPager;
    private TabsAdapterEstudio mAdapter;
    private ActionBar actionBar;
    private SharedPreferences sharedpreferences;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_estudio);

        // Initilization
        sharedpreferences = this.getSharedPreferences(Constantes.MY_PREFERENCES, Context.MODE_PRIVATE);
        viewPager = (ViewPager) findViewById(R.id.vPagerEstudio);
        actionBar = getActionBar();

        mAdapter = new TabsAdapterEstudio(getSupportFragmentManager(), sharedpreferences);
        viewPager.setAdapter(mAdapter);

        actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
        actionBar.setDisplayHomeAsUpEnabled(true);
        actionBar.setBackgroundDrawable(new ColorDrawable(Color.rgb(66, 139, 202)));
        // Adding Tabs
        for (String tab_name : new String[]{"Electrocadiograma", "Airflow",
                "Oximetro", "Posición corporal", "Temperatura"}) {
            actionBar.addTab(actionBar.newTab().setText(tab_name).setTabListener(this));
        }

        /**
         * on swiping the viewpager make respective tab selected
         * */
        viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageSelected(int position) {
                // on changing the page
                // make respected tab selected
                actionBar.setSelectedNavigationItem(position);
            }

            @Override
            public void onPageScrolled(int arg0, float arg1, int arg2) {
            }

            @Override
            public void onPageScrollStateChanged(int arg0) {
            }
        });
    }


    @Override
    protected void onResume() {
        sharedpreferences = getSharedPreferences(Constantes.MY_PREFERENCES, Context.MODE_PRIVATE);
        if (!sharedpreferences.contains(Constantes.NICK_KEY) && !sharedpreferences.contains(Constantes.PASS_KEY)) {
            Intent i = new Intent(this, LoginActivity.class);
            startActivity(i);
        }
        super.onResume();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    // The action bar home/up action should open or close the drawer.
    // ActionBarDrawerToggle will take care of this.
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        Intent intent;
        switch (item.getItemId()) {
            //Logout Aplicacion HealthLC
            case R.id.action_logout:
                SharedPreferences.Editor editor = sharedpreferences.edit();
                editor.clear();
                editor.commit();
                intent = new Intent(this, LoginActivity.class);
                intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                startActivity(intent);
                break;
        }
        return false;
    }

    @Override
    public void onTabSelected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {
        viewPager.setCurrentItem(tab.getPosition());
    }

    @Override
    public void onTabUnselected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {

    }

    @Override
    public void onTabReselected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {

    }
}
